#!/bin/bash
CTYPE=$1
NVER=$2

VER=`sed -n '/version\ =/p' ./cfg.py | awk '{print $3}'`

VER=${VER#\"}
VER=${VER%\"}

FIRST=`echo $VER | awk 'BEGIN { FS="." }{print $1}'`
SECOND=`echo $VER | awk 'BEGIN { FS="." }{print $2}'`

Green=$'\e[1;32m'
Normal=$'\e[0m'

P_SERVER="y"
P_CLIENT="y"
P_DONE="y"

if [ -z $NVER ]
then
    NEW_VER=$[ $SECOND + 1 ]
else
    NEW_VER=$NVER
fi

if [ -z $CTYPE ]
then
    P_CLIENT="n"
fi

echo

if [ $P_DONE == "n" ]
then
    exit 1
fi

echo
echo "update to: $Green$FIRST.$NEW_VER$Normal"
echo "  old ver: $FIRST.$SECOND"

sed -i "s/$FIRST.$SECOND/$FIRST.$NEW_VER/" ./cfg.py

sed -i "s/\"ver\": \"$FIRST.$SECOND\"/\"ver\": \"$FIRST.$NEW_VER\"/" ./cfg/setting.json

rm -rf ./output

TDIR=mes_$FIRST.$NEW_VER

mkdir -p ./output/$TDIR

if [ $P_SERVER == "y" ]
then
    python ./wnd.py
fi

cd output/$TDIR
    
echo
echo -e "...................................copy mestool"
cp -r ~/gmes/deploy/cmd/gen/mestool .

if [ $P_CLIENT == "y" ]
then
    echo
    echo "copy client $Green $CTYPE $Normal"

    cp -r ~/gmes/app/$CTYPE/deploy/pj/release/output/client/ .
fi

cd ..

FILENAME=mes_$FIRST.$NEW_VER.tar.gz

tar cjf $FILENAME ./$TDIR

echo
echo "the update file is: $Green$FILENAME$Normal"
echo

CUR=`date +%H:%M:%S`
echo "finish time: $Green$CUR$Normal"
echo

echo `ls -lh .`
echo
